clear
set more off

global lecmen    "\\intra\partages\au_amic2\SRCV\TABLES_INITIALES\INPUT\MENAGES"
global lecmensas "\\intra\partages\au_amic2\SRCV_AVRIL2021\TABLES_INITIALES_AVRIL2021\MENAGES"
global ecrmen    "\\intra\partages\au_amic2\SRCV_AVRIL2021\BASES_FEV2022_V5\MENAGES"


/* 1 - EXTRACTION DES VARIABLES SELECTIONNéES */
/*     fichier STATA 1ère livraison 2004-2008 */
foreach an of numlist 8  {
cd "$lecmen"
use menages0`an'_diff.dta, clear
keep 	men pond_* idmenc_* ///
		hy090n hy040n hy100n hy050n hy070n hy060n hy080n hy130n hy110n hy145n hy120n hy020 
gen annee_SRCV = 200`an'
order men annee_SRCV 
cd "$ecrmen"
save men_`an'_bis.dta, replace
display `an'
count 
}

foreach an of numlist 5/7  {
cd "$lecmen"
use menages0`an'_diff.dta, clear
keep 	men pond_* idmenc_* ///
		hy090n hy040n hy100n hy050n hy070n hy060n hy080n hy130n hy110n hy145n hy120n hy020 
gen annee_SRCV = 200`an'
order men annee_SRCV 
cd "$ecrmen"
save men_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 4  {
cd "$lecmen"
use menages0`an'_diff.dta, clear
keep 	men /*pond_**/ idmenc_* ///
		hy090n hy040n hy100n hy050n hy070n hy060n hy080n hy130n hy110n hy145n hy120n hy020 
gen annee_SRCV = 200`an'
order men annee_SRCV 
cd "$ecrmen"
save men_`an'.dta, replace
display `an'
count 
}


/* 2 - CHANGEMENT DE NOM DES VARIABLES */
foreach an of numlist 4 {
cd "$ecrmen"
use men_`an'.dta, clear
rename hy090n HY090N
rename hy040n HY040N
rename hy100n HY100N
rename hy050n HY050N
rename hy070n HY070N
rename hy060n HY060N
rename hy080n HY080N
rename hy130n HY130N
rename hy110n HY110N
rename hy145n HY145N
rename hy120n HY120N
rename hy020  HY020
rename idmenc_* idmenc
*rename pond_* pond_men
save men_`an'.dta, replace
} 

foreach an of numlist 5 / 7 {
cd "$ecrmen"
use men_`an'.dta, clear
rename hy090n HY090N
rename hy040n HY040N
rename hy100n HY100N
rename hy050n HY050N
rename hy070n HY070N
rename hy060n HY060N
rename hy080n HY080N
rename hy130n HY130N
rename hy110n HY110N
rename hy145n HY145N
rename hy120n HY120N
rename hy020  HY020
rename idmenc_* idmenc
rename pond_* pond_men
save men_`an'.dta, replace
}

foreach an of numlist 8 {
cd "$ecrmen"
use men_`an'_bis.dta, clear
rename hy090n HY090N
rename hy040n HY040N
rename hy100n HY100N
rename hy050n HY050N
rename hy070n HY070N
rename hy060n HY060N
rename hy080n HY080N
rename hy130n HY130N
rename hy110n HY110N
rename hy145n HY145N
rename hy120n HY120N
rename hy020  HY020
rename idmenc_* idmenc
rename pond_* pond_men
save men_`an'_bis.dta, replace
}


/* 3 - EMPILEMENT DES TABLES */
clear 
use "$ecrmen\men_8_bis.dta", clear
append using 	"$ecrmen\men_4.dta" "$ecrmen\men_5.dta" "$ecrmen\men_6.dta" "$ecrmen\men_7.dta"
count /* 49 796 */
order idmenc annee_SRCV men
sort idmenc annee_SRCV men				
save "$ecrmen\menages_2004_2008.dta", replace
des
tab annee_SRCV 


/* 4 - LABELS */
clear 
use "$ecrmen\menages_2004_2008.dta", clear
label variable men 			"Identifiant longitudinal du menage" 
label variable annee_SRCV	"annee de l'enquete/du fichier"
label variable pond_men		"Ponderation transversale menages"
label variable HY040N		"Revenu net tire de la location de propriete ou terres"
label variable HY050N		"Montant net redresse des alloc. famille/enfants"
label variable HY060N		"Montant net des alloc. d'exclusion sociale non classées ailleurs"
label variable HY070N		"Montant net redresse des aides au logement"
label variable HY080N		"Transferts nets percus entre menages (loyers, pensions)"
label variable HY090N		"Montant annuel net interets, div., benef. tires d'invest. en capital"
label variable HY100N		"Montant annuel net des remboursements interets hypothecaires"
label variable HY110N		"Revenus nets percus par personnes moins 16 ans"
label variable HY120N		"Montant net impot fortune"
label variable HY130N		"Montant net transfert inter-menages verses regulierement en especes"
label variable HY145N		"Rembours./encaissements lies a ajustement d'impot"
label variable HY020		"Revenu total disponible du menage"
sort men annee_SRCV idmenc
order men annee_SRCV idmenc
save "$ecrmen\menages_2004_2008.dta", replace
clear 
use "$ecrmen\menages_2004_2008.dta", clear
